package com.imonsoft.sqm.web.sys;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.sys.Function;
import com.imonsoft.sqm.pojo.sys.Role;
import com.imonsoft.sqm.service.sys.RoleService;
import com.imonsoft.sqm.validator.sys.RoleValidator;

@Controller
@RequestMapping("/sys/role")
public class RoleController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleValidator roleValidator;

	@RequestMapping("/listUI.do")
	public void listUI(ModelMap model) {
	}

	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, Role role) {
		DataGrid<Role> dataGrid = new DataGrid<Role>();
		try {
			dataGrid = roleService.getDataGrid(role, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}

	@RequestMapping("/addUI.do")
	public void addUI(ModelMap model) {
	}

	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, Role role, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			roleValidator.methodAddValidate(role, result);
			if (result.hasErrors()) {
				message.setObj(roleValidator.getErrors(result));
			} else {
				roleService.insert(role);
				message.setSuccess(true);
				message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}
		} catch (SystemException se) {
			logger.debug(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id) {
		if (null != id) {
			Role role = roleService.get(id);
			List<Function> functionList = roleService.getFunctionListByRoleId(id);
			String ids = "";
			if (functionList != null) {
				for (Function function : functionList) {
					ids += function.getCode() + ",";
				}
				ids = ids.substring(0, ids.length() - 1);
			}
			model.put("role", role);
			model.put("ids", ids);
		} else {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
	}

	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response, Role role, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			roleValidator.methodModifyValidate(role, result);
			if (result.hasErrors()) {
				message.setObj(roleValidator.getErrors(result));
			} else {
				roleService.update(role);
				message.setSuccess(true);
				message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}

		} catch (SystemException se) {
			logger.debug(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response, String[] ids) {
		ReturnMessage message = new ReturnMessage();
		try {
			List<Long> idList = new ArrayList<Long>();
			for (String id : ids) {
				idList.add(Long.valueOf(id));
			}
			roleService.deleteRole(idList);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
}
